<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Event_category_tournament_entity{
	var $id = 0;
	var $category = "";
	var $tournament_id = 0;
	var $status = "";
	var $is_root = "";
	var $ordering = 0;
	var $is_open = "yes";
	var $key_id = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_CATEGORY_TOURNAMENT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->category = $result[0]->category;
			$this->tournament_id = $result[0]->tournament_id;
			$this->status = $result[0]->status;
			$this->is_root = $result[0]->is_root;
			$this->ordering = $result[0]->ordering;
			$this->is_open = $result[0]->is_open;
			$this->key_id = $result[0]->key_id;
			return $this;
		}
		return false;
	}
	
	function setCategory($input){ $this->category=$input; }
	function setTournamentId($input){ $this->tournament_id=$input;}
	function setStatus($input){ $this->status=$input; }
	function setIsRoot($input){ $this->is_root=$input; }
	function setOrdering($input){ $this->ordering=$input; }
	function setIsOpen($input){ $this->is_open=$input; }
	function setKeyId($input){ $this->key_id=$input; }
	
	function getId(){ return $this->id; }
	function getCategory(){ return $this->category; }
	function getTournamentId(){ return $this->tournament_id; }
	function getStatus(){ return $this->status; }
	function getIsRoot(){ return $this->is_root; }
	function getOrdering(){ return $this->ordering; }
	function getIsOpen(){ return $this->is_open; }
	function getKeyId(){ return $this->key_id; }
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('category', $this->category);
		$ci->db->set('tournament_id', $this->tournament_id);
		$ci->db->set('status', $this->status);
		$ci->db->set('is_root', $this->is_root);
		$ci->db->set('ordering', $this->ordering);
		$ci->db->set('is_open', $this->is_open);
		$ci->db->set('key_id', $this->key_id);
		$ci->db->where('id', $this->id)->update(TBL_CATEGORY_TOURNAMENT);
	}
	
	function createNewEntity($tournament_id){
		$ci=& get_instance();
		$ci->db->set('category', '');
		$ci->db->set('key_id', $this->generate_cat_key_id());
		$ci->db->set('ordering', $this->getNextOrdering($tournament_id));
		$ci->db->insert(TBL_CATEGORY_TOURNAMENT);
		
		return $ci->db->insert_id();
	}
	
	function getNextOrdering($tournament_id){
		$ci=& get_instance();
		$count = count( array_filter( $ci->db->where('tournament_id', $tournament_id)->get(TBL_CATEGORY_TOURNAMENT)->result() ) );
		return $count+1;
	}
	
	function generate_cat_key_id(){
		$ci=& get_instance();
		$records = $ci->db->get(TBL_CATEGORY_TOURNAMENT)->result();
		$numberArray = array();
		foreach($records as $obj){
			$numberArray[] = intval(substr($obj->key_id,1,strlen($obj->key_id)));
		}
		 
		while(true){
			$rand = $ci->digit->rand_digit(9); 
			if(!in_array($rand, $numberArray)){
				return "T".$rand;
			}
		}
	}
	
	function getArrayListOfEventCategory($tournament_id){
		$ci=& get_instance();
		return $ci->db->where('tournament_id', $tournament_id)->order_by('ordering','desc')->get(TBL_CATEGORY_TOURNAMENT)->result();
	}
	
	function getRootEventCategory($tournament_id){
		$ci=& get_instance();
		$tmp= array();
		foreach($this->getArrayListOfEventCategory($tournament_id) as $item){
			if($item->is_root == 'yes'){
				$tmp[] = $item;
				break;
			}
		}
		return $tmp;
	}
	
	function getNotRootEventCategory($tournament_id){
		$ci=& get_instance();
		$tmp= array();
		foreach($this->getArrayListOfEventCategory($tournament_id) as $item){
			if($item->is_root != 'yes'){
				$tmp[] = $item;
			}
		}
		return $tmp;
	}
	
	function getArrayIdAndNameOfEventCategory($tournament_id){
		$array = array();
		foreach($this->getArrayListOfEventCategory($tournament_id) as $obj){
			$array[$obj->id] = $obj->category;
		}
		return $array;
	}
	
	function getLivingCategoryEvent(){
		$ci=& get_instance();
		return $ci->db->where('status','living')->get(TBL_CATEGORY_TOURNAMENT)->result();
	}
	
	function changeStatusEventCategoryRealTime(){
		$ci=& get_instance();
		$closed = true;
		foreach($this->getLivingCategoryEvent() as $item){
			foreach($ci->event_entity->getArrayListOfEvent($item->id) as $obj){
				if($obj->status != "closed"){
					$closed = false;
					break;
				}
			}
			if($closed){
				$ci->db->where('id',$item->id)->update(TBL_CATEGORY_TOURNAMENT, array('status'=>'closed'));
			}
		}					
	}
	
	function checkTournamentBettingEnable($tournament_id){
		$array = $this->getArrayListOfEventCategory($tournament_id);
		$enable = false;
		foreach($array as $item){
			if($item->is_open == 'yes'){
				return true;
			}
		}
		return $enable;
	}
	
	function getEnableCategories($tournament_id){
		$ci=& get_instance();
		return $ci->db->where( array('status'=>'living','is_open'=>'yes','tournament_id'=>$tournament_id) )->order_by('ordering','desc')->get(TBL_CATEGORY_TOURNAMENT)->result();
	}
	
	//end class
}
